﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ChinapnrAPI;

namespace Uranus.ChinaService.Receives
{
    public class LoansRepaymentReceive : BaseReceive
    {
        public LoansRepaymentReceive(HttpContext httpContext)
            : base(httpContext)
        {
            
        }

        /// <summary>
        /// 放款回调操作锁
        /// </summary>
        private static object LockLoans = new object();

        /// <summary>
        /// 放款回调
        /// </summary>
        public void Loans()
        {
            lock (LockLoans)
            {
                ChinapnrTransaction chinapnrTransaction = new ChinapnrTransaction();
                try
                {
                    if (!chinapnrTransaction.VerLoans(this.Request))
                    {
                        WriteErrorLog("放款回调验证失败", string.Format("OrdId:{0},RespCode:{1},RespDesc:{2}", this.OrdId, this.RespCode, this.RespDesc));
                    }
                    else
                    {
                        string result = string.Format("订单号:{0} 入账账户:{1} 出账账户:{2} 入账金额:{3} 手续费:{4} 所属投标id:{5}",
                            this.OrdId, this.InCustId, this.OutCustId, this.TransAmt, this.Fee, this.SubOrdId);
                        WriteInfoLog("放款回调成功", result);

                        Response.Write("RECV_ORD_ID_" + this.OrdId);
                    }
                }
                catch (Exception ex)
                {
                    string error = string.Format("订单号:{0},异常:{1}", this.OrdId, ex.ToString());
                    WriteErrorLog("放款回调执行异常", error, null, true);
                }
            }
        }

        /// <summary>
        /// 还款回调操作锁
        /// </summary>
        private static object LockRepayment = new object();

        /// <summary>
        /// 还款回调
        /// </summary>
        public void Repayment()
        {
            lock (LockRepayment)
            {
                ChinapnrTransaction chinapnrTransaction = new ChinapnrTransaction();
                try
                {
                    if (Request.Form["PrincipalAmt"] != null && !string.IsNullOrEmpty(Request.Form["PrincipalAmt"].ToString()) ? chinapnrTransaction.VerRepayment30(Request) : chinapnrTransaction.VerRepayment(Request))
                    {
                        string result = string.Format("订单号:{0} 成功还款 入账账户:{1} 出账账户:{2} 入账金额:{3} 手续费:{4} 所属投标id:{5}",
                            this.OrdId, this.InCustId, this.OutCustId, this.TransAmt, this.Fee, this.SubOrdId);
                        WriteInfoLog("还款回调成功", result);
                        Response.Write("RECV_ORD_ID_" + this.OrdId);
                    }
                    else
                    {
                        WriteErrorLog("还款回调验证失败", string.Format("OrdId:{0},RespCode:{1},RespDesc:{2}", this.OrdId, this.RespCode, this.RespDesc));
                    }
                }
                catch (Exception ex)
                {
                    string error = string.Format("订单号:{0},异常:{1}", this.OrdId, ex.ToString());
                    WriteErrorLog("还款回调执行异常", error, null, true);
                }
            }
        }
    }
}